package com.budwk.app.sqfwDoc.controller;

import com.budwk.app.sqfwDoc.models.SqfwTemplateDict;
import com.budwk.app.sqfwDoc.models.SqfwTemplateVisDict;
import com.budwk.app.sqfwDoc.service.SqfwTemplateVisDictService;
import com.budwk.app.sqfwDoc.vo.SqfwTemplateVisDictSimpleVO;
import com.budwk.app.sqfwDoc.vo.SqfwTemplateVisDictTreeVO;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author ZY.Wang
 * @since 2023/12/11
 */
@IocBean
@At("/platform/sqfw/sqfwTemplateVisDict")
@ApiDefinition(tag = "社区回显字典")
@Slf4j
public class SqfwTemplateVisDictController {

    @Inject
    private SqfwTemplateVisDictService sqfwTemplateVisDictService;

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "查询回显字段树")
    public Result<?> listVisDictTree() {
        List<SqfwTemplateVisDictTreeVO> list = sqfwTemplateVisDictService.listVisDictTree();
        return Result.data(list);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "根据parentId查询树")
    public Result<?> listDictByParentId(@Param("parentId") String parentId) {
        if (StringUtils.isBlank(parentId)) parentId = "0";
        Cnd cnd = Cnd.NEW();
        cnd.and(SqfwTemplateDict::getParentId, "=", parentId);
        return Result.success(sqfwTemplateVisDictService.query(cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "添加回显字段")
    public Result<?> addDict(@Param("..") SqfwTemplateVisDict data) {
        String userId = SecurityUtil.getUserId();
        if (StringUtils.isBlank(data.getParentId())) data.setParentId("0");
        data.setCreateBy(userId);
        data.setCreateTime(new Date());
        data.setUpdateBy(userId);
        data.setUpdateTime(new Date());
        sqfwTemplateVisDictService.insert(data);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "添加回显字段")
    public Result<?> addDictBatch(@Param("list") List<SqfwTemplateVisDict> dataList) {
        String userId = SecurityUtil.getUserId();

        for (SqfwTemplateVisDict data : dataList) {
            if (StringUtils.isBlank(data.getParentId())) data.setParentId("0");
            data.setCreateBy(userId);
            data.setCreateTime(new Date());
            data.setUpdateBy(userId);
            data.setUpdateTime(new Date());
            sqfwTemplateVisDictService.insert(data);
        }
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "更新回显字段")
    public Result<?> updateDict(@Param("..") SqfwTemplateVisDict data) {
        String userId = SecurityUtil.getUserId();
        data.setUpdateBy(userId);
        data.setUpdateTime(new Date());
        sqfwTemplateVisDictService.updateIgnoreNull(data);
        return Result.success();
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获取回显字段详情")
    public Result<?> getDetail(@Param("id") String id) {
        SqfwTemplateVisDictSimpleVO vo = sqfwTemplateVisDictService.getDetail(id);
        return Result.success(vo);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "删除回显字段")
    public Result<?> deleteDict(@Param("id") String id) {
        sqfwTemplateVisDictService.delete(id);
        return Result.success();
    }
}
